﻿using System;
using System.Collections.Generic;
using System.Windows.Controls;
using System.Linq;
using System.Windows;
using System.Collections.ObjectModel;

namespace QuanLyDuocPham.Controllers.QuanLyHeThong.DanhMuc
{
    class DUOCPHAM_Control : IDisposable
    {
        DuocPhamDataContext db = new DuocPhamDataContext(staticClass.Conn);
        public void Dispose()
        {
            if (db != null)
            {
                db.Dispose();
                db = null;
            }
        }
        public void loadComboBox(ComboBox cmb)
        {

            cmb.Items.Clear();
            var types = db.DUOCPHAM_Get_List();

            var typeorder = types.OrderBy(w => w.TEN_DUOCPHAM);
            foreach (var t in typeorder)
            {
                cmb.Items.Add(t);
            }

            cmb.SelectedValuePath = "MA_DUOCPHAM";
            cmb.IsTextSearchEnabled = true;
            cmb.IsTextSearchCaseSensitive = false;
            cmb.IsEditable = true;

            DUOCPHAM_Get_ListResult temp = new DUOCPHAM_Get_ListResult();

            temp.MA_DUOCPHAM = "...";
            temp.TEN_DUOCPHAM = "Thêm mới";
            cmb.Items.Add(temp);
        }

        public void loadComboBoxBanLe(ComboBox cmb, TextBox txtKho)
        {
            cmb.Items.Clear();
            var list_lo = db.LO_GetByMaKho(txtKho.Text);
            var list_dp = db.DUOCPHAM_Get_List().Join(db.DONVITINH_Get_List(), p => p.MA_DVT, c => c.MA_DVT, (p, c) => new { MA_DUOCPHAM = p.MA_DUOCPHAM, TEN_DUOCPHAM = p.TEN_DUOCPHAM, MA_DVTA = p.MA_DVT, TEN_DVTA = c.DIEN_GIAI, SL_QUYDOI = c.SO_LUONG  });
            foreach (var t in list_dp)
            {
                if (t.SL_QUYDOI == 1)
                {
                    int soluong = 0;
                    foreach (var u in list_lo)
                    {

                        var list_dpinlo = db.LO_CT_GetByMaKhoMaLoMaDuocpham(txtKho.Text, t.MA_DUOCPHAM, u.MA_LO);
                        if (list_dpinlo.Count() > 0)
                        {
                            var dp = list_dpinlo.Single();
                            if (dp.HANSUDUNG > DateTime.Now && dp.SO_LUONG > 0)
                            {
                                soluong = soluong + int.Parse(dp.SO_LUONG.ToString());
                            }
                        }
                    }
                    if (soluong > 0)
                    {
                        cmb.Items.Add(new { MA_DUOCPHAM = t.MA_DUOCPHAM, TEN_DUOCPHAM = t.TEN_DUOCPHAM, MA_DVT = t.MA_DVTA, TEN_DVT = t.TEN_DVTA, SO_LUONG = soluong });
                    }
                }
            }
        }

        public void loadComboBoxBanSi(ComboBox cmb, TextBox txtKho)
        {
            cmb.Items.Clear();
            var list_lo = db.LO_GetByMaKho(txtKho.Text);
            var list_dp = db.DUOCPHAM_Get_List().Join(db.DONVITINH_Get_List(), p => p.MA_DVT, c => c.MA_DVT, (p, c) => new { MA_DUOCPHAM = p.MA_DUOCPHAM, TEN_DUOCPHAM = p.TEN_DUOCPHAM, MA_DVTA = p.MA_DVT, TEN_DVTA = c.DIEN_GIAI, SL_QUYDOI = c.SO_LUONG });
            foreach (var t in list_dp)
            {
                if (t.TEN_DVTA.Trim().ToLower() =="thùng")
                {
                    int soluong = 0;
                    foreach (var u in list_lo)
                    {

                        var list_dpinlo = db.LO_CT_GetByMaKhoMaLoMaDuocpham(txtKho.Text, t.MA_DUOCPHAM, u.MA_LO);
                        if (list_dpinlo.Count() > 0)
                        {
                            var dp = list_dpinlo.Single();
                            if (dp.HANSUDUNG > DateTime.Now && dp.SO_LUONG > 0)
                            {
                                soluong = soluong + int.Parse(dp.SO_LUONG.ToString());
                            }
                        }
                    }
                    if (soluong > 0)
                    {
                        cmb.Items.Add(new { MA_DUOCPHAM = t.MA_DUOCPHAM, TEN_DUOCPHAM = t.TEN_DUOCPHAM, MA_DVT = t.MA_DVTA, TEN_DVT = t.TEN_DVTA, SO_LUONG = soluong });
                    }
                }
            }
        }

        public string loadID_moi()
        {
            string ID = "";

            int rowcount = db.DUOCPHAM_Get_List().Count();
            if (rowcount == 0)
            {
                ID = "DP00000001";
            }
            else
            {
                for (int i = 1; i <= rowcount+1; i++)
                {
                    string IDtam = "DP";
                    for (int j = 0; j < (8 - i.ToString().Length); j++)
                        IDtam = IDtam + "0";
                    IDtam = IDtam + i;
                    Boolean kq = true;
                    var data = db.DUOCPHAM_Get_List();

                    foreach (var t in data)
                    {

                        if (t.MA_DUOCPHAM.Trim() == IDtam.Trim())

                            kq = false;
                    }
                    if (kq == true)
                    {
                        ID = IDtam;
                        break;
                    }
                }

            }
            return ID;

        }


        public void addData(string ma_dp,ComboBox cmbMa_DVT, ComboBox cmbMa_Nhom_VT, ComboBox cmbMa_Thue, ComboBox cmbMa_NSX, ComboBox cmbMa_NCC, TextBox txtTen_Biet_Duoc, TextBox txtTon_NN, TextBox txtTon_LN, TextBox txtPhan_Loai, CheckBox chbDoc_Duoc, TextBox txtHDSD, TextBox txtHanDung, TextBox txtDongGoi, TextBox txtNongDo, TextBox txtSDK, TextBox txtNhietDo_BaoQuan, CheckBox chbAS_Mat_Troi, CheckBox chbTranh_Am_Uot, CheckBox chbTranh_Dong_Lanh, CheckBox chbhTranh_Nhiet_Do_Cao, TextBox txtCongDung,  TextBox txtGhi_Chu, TextBox txtTem)
        {
            if (validateData(cmbMa_DVT, cmbMa_Nhom_VT, cmbMa_Thue, cmbMa_NSX, cmbMa_NCC, txtTen_Biet_Duoc, txtTon_NN, txtTon_LN, txtPhan_Loai, txtHDSD, txtHanDung, txtSDK, txtNhietDo_BaoQuan, txtCongDung)==false)
                MessageBox.Show("Chưa đủ thông tin!");
            else
            {
                var Ma_DVT = (DONVITINH_Get_ListResult)cmbMa_DVT.SelectedItem;
                var Ma_Nhom_VT = (NHOMDP_Get_ListResult)cmbMa_Nhom_VT.SelectedItem;
                var Ma_Thue = (THUESUAT_Get_ListResult)cmbMa_Thue.SelectedItem;
                var Ma_NSX = (NSX_Get_ListResult)cmbMa_NSX.SelectedItem;
                var Ma_NCC = (NCC_Get_ListResult)cmbMa_NCC.SelectedItem;

                db.DUOCPHAM_Insert(ma_dp, cmbMa_Thue.SelectedValue.ToString(), cmbMa_NCC.SelectedValue.ToString(), cmbMa_NSX.SelectedValue.ToString(), cmbMa_Nhom_VT.SelectedValue.ToString(), cmbMa_DVT.SelectedValue.ToString(), txtTen_Biet_Duoc.Text, int.Parse(txtTon_NN.Text), int.Parse(txtTon_LN.Text), txtPhan_Loai.Text, chbDoc_Duoc.IsChecked, txtHDSD.Text, txtHanDung.Text, "", txtDongGoi.Text, txtNongDo.Text, txtSDK.Text, txtNhietDo_BaoQuan.Text, chbAS_Mat_Troi.IsChecked, chbTranh_Am_Uot.IsChecked, chbTranh_Dong_Lanh.IsChecked, chbhTranh_Nhiet_Do_Cao.IsChecked, txtCongDung.Text, txtGhi_Chu.Text, double.Parse(txtTem.Text));
                MessageBox.Show("Đã thêm thành công");
            }
        }


        public void loadData(ListView List)
        {
            List.Items.Clear();
            var types = db.DUOCPHAM_Get_List_Full();
            var typeorder = types.OrderBy(w => w.TEN_DUOCPHAM);
            foreach (var t in typeorder)
            {
                if (t.DIEN_GIAI.Trim().ToLower() == "thùng")
                    List.Items.Add(t);
            }
        }
        public void loadData(DataPager dp)
        {
            ObservableCollection<Object> dpSource = new ObservableCollection<Object>();
            int i = 0;
            var types = db.DUOCPHAM_Get_List_Full();
            var typeorder = types.OrderBy(w => w.TEN_DUOCPHAM);
            foreach (var u in typeorder)
            {
                if (u.TEN_DUOCPHAM.Trim() != "" && u.DIEN_GIAI.ToLower()=="thùng")
                {
                    dpSource.Add(new { STT = i, u.MA_DUOCPHAM, u.PHAN_LOAI, u.TEN_DUOCPHAM, u.TEN_NHOM });
                    i++;
                }
            }
            dp.ItemsSource = dpSource;
            dp.ItemsPerPage = 10;
        }


        public void loadData(ListBox List)
        {

            var types = db.DUOCPHAM_Get_List_Full();
            var typeorder = types.OrderBy(w => w.TEN_DUOCPHAM);
            foreach (var t in typeorder)
            {
                List.Items.Add(t);
            }

            List.DisplayMemberPath = "TEN_DUOCPHAM";
            List.SelectedValuePath = "MA_DUOCPHAM";
        }

        public void findData(TextBox txtSearch, ListBox list1)
        {
            if (txtSearch.Text.Trim() != null)
            {
                list1.ItemsSource = null;
                list1.Items.Clear();
                var types = db.DUOCPHAM_GetByTenDuocPham(txtSearch.Text);
                var typeorder = types.OrderBy(w => w.TEN_DUOCPHAM);
                foreach (var t in typeorder)
                {
                    list1.Items.Add(t);
                }
                
            }
            else 
            {
                loadData(list1);
            }
        }

        public void updateData(string ma_dp, ComboBox cmbMa_DVT, ComboBox cmbMa_Nhom_VT, ComboBox cmbMa_Thue, ComboBox cmbMa_NSX, ComboBox cmbMa_NCC, TextBox txtTen_Biet_Duoc, TextBox txtTon_NN, TextBox txtTon_LN, TextBox txtPhan_Loai, CheckBox chbDoc_Duoc, TextBox txtHDSD, TextBox txtHanDung, TextBox txtDongGoi, TextBox txtNongDo, TextBox txtSDK, TextBox txtNhietDo_BaoQuan, CheckBox chbAS_Mat_Troi, CheckBox chbTranh_Am_Uot, CheckBox chbTranh_Dong_Lanh, CheckBox chbhTranh_Nhiet_Do_Cao, TextBox txtCongDung, TextBox txtGhi_Chu, TextBox txtTem)
        {
            if (validateData(cmbMa_DVT, cmbMa_Nhom_VT, cmbMa_Thue, cmbMa_NSX, cmbMa_NCC, txtTen_Biet_Duoc, txtTon_NN, txtTon_LN, txtPhan_Loai, txtHDSD, txtHanDung, txtSDK, txtNhietDo_BaoQuan, txtCongDung)==false)
                MessageBox.Show("Chưa đủ thông tin!");
            else
            {

                db.DUOCPHAM_Update(ma_dp,ma_dp, cmbMa_Thue.SelectedValue.ToString(), cmbMa_NCC.SelectedValue.ToString(), cmbMa_NSX.SelectedValue.ToString(), cmbMa_Nhom_VT.SelectedValue.ToString(), cmbMa_DVT.SelectedValue.ToString(), txtTen_Biet_Duoc.Text, int.Parse(txtTon_NN.Text), int.Parse(txtTon_LN.Text), txtPhan_Loai.Text, chbDoc_Duoc.IsChecked, txtHDSD.Text, txtHanDung.Text, "", txtDongGoi.Text, txtNongDo.Text, txtSDK.Text, txtNhietDo_BaoQuan.Text, chbAS_Mat_Troi.IsChecked, chbTranh_Am_Uot.IsChecked, chbTranh_Dong_Lanh.IsChecked, chbhTranh_Nhiet_Do_Cao.IsChecked, txtCongDung.Text, txtGhi_Chu.Text, double.Parse(txtTem.Text));
            MessageBox.Show("Đã cập nhật!");
            }
        }

        public string deleteData(string ID_Delete, string error)
        {


            error = "";
            if (db.DONTHUOC_CT_GetByMaDuocpham(ID_Delete).Count() > 0 || db.QUANHE_GetByMaDuocpham(ID_Delete).Count() > 0 || db.QUANHE_GetByDuoMaDuocpham(ID_Delete).Count() > 0 || db.PHIEUNHAP_CT_GetByMaDuocpham(ID_Delete).Count() > 0 ||  (ID_Delete).Count() > 0 || db.CO_HOAT_CHAT_GetByMaDuocpham(ID_Delete).Count() > 0 || db.GIA_GetByMaDuocpham(ID_Delete).Count() > 0 || db.THUOC_KHO_GetByMaDuocpham(ID_Delete).Count() > 0)
                error = "Không thể xóa";
            else
            {
                db.DUOCPHAM_Delete(ID_Delete);
                error = "Xóa thành công!";
            }
            return error;

        }

        DONVITINH_Control ctrl1 = new DONVITINH_Control();
        NHOMDP_Control ctrl2 = new NHOMDP_Control();
        THUESUAT_Control ctrl3 = new THUESUAT_Control();
        NCC_Control ctrl4 = new NCC_Control();
        NSX_Control ctrl5 = new NSX_Control();
        public void loadForm_Edit(string ID_Edit, ComboBox cmbMa_DVT, ComboBox cmbMa_Nhom_VT, ComboBox cmbMa_Thue, ComboBox cmbMa_NSX, ComboBox cmbMa_NCC, TextBox txtTen_Biet_Duoc, TextBox txtTon_NN, TextBox txtTon_LN, TextBox txtPhan_Loai, CheckBox chbDoc_Duoc, TextBox txtHDSD, TextBox txtHanDung, TextBox txtDongGoi, TextBox txtNongDo, TextBox txtSDK, TextBox txtNhietDo_BaoQuan, CheckBox chbAS_Mat_Troi, CheckBox chbTranh_Am_Uot, CheckBox chbTranh_Dong_Lanh, CheckBox chbhTranh_Nhiet_Do_Cao, TextBox txtCongDung, TextBox txtGhi_Chu, TextBox txtTem)
        {

            var sl = (DUOCPHAM_GetByMaDuocphamResult)db.DUOCPHAM_GetByMaDuocpham(ID_Edit).Single();


            ctrl1.loadComboBox(cmbMa_DVT);
            cmbMa_DVT.SelectedValue = sl.MA_DVT;

            ctrl2.loadComboBox(cmbMa_Nhom_VT);
            
            cmbMa_Nhom_VT.SelectedValue = sl.MA_NHOMDP;


            ctrl3.loadComboBox(cmbMa_Thue);

            cmbMa_Thue.SelectedValue = sl.MA_THUESUAT;


            ctrl4.loadComboBox(cmbMa_NCC);

            cmbMa_NCC.SelectedValue = sl.MA_NCC;

            ctrl5.loadComboBox(cmbMa_NSX);

            cmbMa_NSX.SelectedValue = sl.MA_NSX;     
                    
                     
                     txtTen_Biet_Duoc.Text = sl.TEN_DUOCPHAM;
                     
                     txtTon_NN.Text = sl.TON_NN.ToString();
                     txtTon_LN.Text = sl.TON_LN.ToString();
                     txtPhan_Loai.Text = sl.PHAN_LOAI;
                     chbDoc_Duoc.IsChecked = sl.DOC_DUOC;
                     txtHDSD.Text = sl.HDSD;
                     txtHanDung.Text = sl.HAN_DUNG;
                     
                     txtDongGoi.Text = sl.DONG_GOI;
                     txtNongDo.Text = sl.NONG_DO;
                     txtSDK.Text = sl.SDK;
                     txtNhietDo_BaoQuan.Text = sl.NHIETDO_BAOQUAN;
                     chbAS_Mat_Troi.IsChecked = sl.AS_MAT_TROI;
                     chbTranh_Am_Uot.IsChecked = sl.TRANH_AM_UOT;
                     chbTranh_Dong_Lanh.IsChecked = sl.TRANH_DONG_LANH;
                     chbhTranh_Nhiet_Do_Cao.IsChecked = sl.TRANH_NHIET_DO_CAO;
                     txtCongDung.Text = sl.CONG_DUNG;
                     txtGhi_Chu.Text = sl.GHI_CHU;
                     txtTem.Text = sl.TEM.ToString();
        }

        private bool validateData( ComboBox cmbMa_DVT, ComboBox cmbMa_Nhom_VT, ComboBox cmbMa_Thue, ComboBox cmbMa_NSX, ComboBox cmbMa_NCC, TextBox txtTen_Biet_Duoc, TextBox txtTon_NN, TextBox txtTon_LN, TextBox txtPhan_Loai, TextBox txtHDSD, TextBox txtHanDung,  TextBox txtSDK, TextBox txtNhietDo_BaoQuan, TextBox txtCongDung)
        {
            if (txtTen_Biet_Duoc.Text.Trim() == "" || txtTon_NN.Text.Trim() == "" || txtTon_LN.Text.Trim() == "" || txtPhan_Loai.Text.Trim() == "" || txtHDSD.Text.Trim() == "" || txtHanDung.Text.Trim()=="" || txtSDK.Text.Trim() == "" || txtNhietDo_BaoQuan.Text.Trim() == "" || txtCongDung.Text.Trim() == "" ||
                cmbMa_DVT.SelectedItem == null || cmbMa_Nhom_VT.SelectedItem == null || cmbMa_Thue.SelectedItem == null || cmbMa_NSX.SelectedItem == null  
                || cmbMa_NCC.SelectedItem == null )
            {
                return false;
            }

            return true;
        }
    }
}
